import Vue from 'vue'
import Router from 'vue-router'
// import Home from './views/Home.vue'
import Login from "./components/Login.vue"
import Home from "./components/Home.vue"
import Welcome from "./components/Welcome.vue"
import User from "./components/user/User.vue"
import Roles from "./components/right/Roles.vue"
import Rights from "./components/right/Rights.vue"
import Goods from "./components/product/Goodlist.vue"
import Numcategory from "./components/product/Numcategory.vue"
import Goodcategory from "./components/product/Goodcategory.vue"
import Orders from "./components/order/Orders.vue"
import Reports from "./components/data/Datalist.vue"


Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: User },
        { path: '/roles', component: Roles },
        { path: '/rights', component: Rights },
        { path: '/goods', component: Goods },
        { path: '/params', component: Numcategory },
        { path: '/categories', component: Goodcategory },
        { path: '/orders', component: Orders },
        { path: '/reports', component: Reports },
      ]
    },
    // {
    //   path: '/',
    //   name: 'home',
    //   component: Home
    // },
    // {
    //   path: '/about',
    //   name: 'about',
    //   // route level code-splitting
    //   // this generates a separate chunk (about.[hash].js) for this route
    //   // which is lazy-loaded when the route is visited.
    //   component: () => import(/* webpackChunkName: "about" */ './views/About.vue')
    // }
  ]
})
router.beforeEach((to, from, next) => {
  //to 要去的页面 from从哪个页面来  next放行，next()直接放行 next("/login")直接跳转登录
  if (to.path == "/login") return next();//去登录页面直接放行
  let tokenstr = window.sessionStorage.getItem("token");//拿出token,有就代表登录，没有就肯定没登录
  if (!tokenstr) return next("./login");
  next();
})
export default router
